#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(&myManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(fun(QNetworkReply *)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QString filepath = QFileDialog::getOpenFileName(this, "我的文件对话框");

    QImage myImage(filepath);
    QByteArray byteAr;
    QBuffer buf(&byteAr);
    myImage.save(&buf, "JPG");

    QUrl myUrl("http://ocrcp.market.alicloudapi.com/rest/160601/ocr/ocr_vehicle_plate.json");
    QNetworkRequest myRequest(myUrl);
    QString appCode = "APPCODE 5a242eb4c3214d6cb93140863ada6bfb";
    QString host = "ocrcp.market.alicloudapi.com";
    myRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    myRequest.setRawHeader("Authorization", appCode.toLocal8Bit());
    myRequest.setRawHeader("Host", host.toLocal8Bit());

    QJsonObject jsonObj;
    jsonObj.insert("image", byteAr.toBase64().data());
    jsonObj.insert("configure", "{\"multi_crop\":true}");
    QJsonDocument jsonDoc = QJsonDocument(jsonObj);
    QByteArray post_data = jsonDoc.toJson(QJsonDocument::Compact);
    myManager.post(myRequest, post_data);

    flag = Recog_Plat;
}

void MainWindow::fun(QNetworkReply *rep)
{
    // 读取应答信息
    QByteArray buf = rep->readAll();
    ui->plainTextEdit->appendPlainText(buf);
}

void MainWindow::on_pushButton_3_clicked()
{
    QString filepath = QFileDialog::getOpenFileName(this, "我的文件对话框");

    // 获取输入的身份证号
    QString id = ui->lineEdit->text();

    // 获取输入的姓名
    QString namestr = ui->lineEdit_2->text();

    // 人脸照片，有些供应商要求图片质量要高，背景不要有其它的杂物
    QImage myImage(filepath);
    QByteArray byteAr;
    QBuffer buf(&byteAr);
    myImage.save(&buf, "JPG");

    // 供应商：杭州快证签科技有限公司
    QUrl myUrl("http://kzfacev1.market.alicloudapi.com/api-mall/api/face_id_card_yi_suo/check");

    QNetworkRequest myRequest(myUrl);
    QString appCode = "APPCODE 5a242eb4c3214d6cb93140863ada6bfb";

    myRequest.setRawHeader("Authorization", appCode.toUtf8());
    myRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded; charset=UTF-8");

    // QUrlQuery表示POST请求的查询条件
    QUrlQuery postData;

    // 供应商：杭州快证签科技有限公司
    postData.addQueryItem("idcard", id);               // 身份证号码字符串
    postData.addQueryItem("name", namestr);            // 名字字符串
    postData.addQueryItem("image", byteAr.toBase64()); // 人脸图片base64格式

    myManager.post(myRequest, postData.query(QUrl::FullyEncoded).toUtf8());
    flag = Recog_Face;
}
